import router from './router'
import nProgress from 'nprogress'
import 'nprogress/nprogress.css'
import store from './store'
// 定义白名单
const whiteList = ['/login', '/404']
// 开启前置守卫
router.beforeEach(async(to, from, next) => {
  nProgress.start()
  // 判断token是否存在
  if (store.getters.token) {
    // 有token还去登录页
    if (to.path === '/login') {
      next('/')
      nProgress.done()
    } else { // 登录了不去登录页
      if (store.getters.userId) {
        next()
      } else {
        // 获取用户资料给who？
        // 答：给state的userinfo
        await store.dispatch('user/getUserInfo')
        next()
      }
    }
  } else { // token不存在，那看是否在白名单
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
      nProgress.done()
    }
  }
})
// 开启后置守卫
router.afterEach((to, from, next) => {
  nProgress.done()
})
